package org.itzixi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.itzixi.base.BaseInfoProperties;
import org.itzixi.enums.YesOrNo;
import org.itzixi.mapper.*;
import org.itzixi.pojo.FriendCircle;
import org.itzixi.pojo.FriendCircleLiked;
import org.itzixi.pojo.Friendship;
import org.itzixi.pojo.Users;
import org.itzixi.pojo.bo.FriendCircleBO;
import org.itzixi.pojo.vo.ContactsVO;
import org.itzixi.pojo.vo.FriendCircleVO;
import org.itzixi.service.FriendCircleService;
import org.itzixi.service.FriendshipService;
import org.itzixi.service.UsersService;
import org.itzixi.utils.PagedGridResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class FriendCircleServiceImpl extends BaseInfoProperties implements FriendCircleService {
    @Resource
    private FriendCircleMapper friendCircleMapper;
    @Resource
    private FriendCircleMapperCustom friendCircleMapperCustom;
    @Resource
    private FriendCircleLikedMapper friendCircleLikedMapper;
    @Resource
    private UsersService usersService;

    @Transactional
    @Override
    public void publish(FriendCircleBO friendCircleBO) {

        FriendCircle pendingFriendCircle = new FriendCircle();

        BeanUtils.copyProperties(friendCircleBO, pendingFriendCircle);

        friendCircleMapper.insert(pendingFriendCircle);
    }

    @Transactional
    @Override
    public PagedGridResult queryList(String userId, Integer page, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        Page<FriendCircleVO> pageInfo = new Page<>(page, pageSize);
        friendCircleMapperCustom.queryFriendCircleList(pageInfo,map);

        return setterPagedGridPlus(pageInfo);
    }

    @Transactional
    @Override
    public void like(String friendCircleId, String userId) {
        //查询朋友圈发布者
        FriendCircle friendCircle = this.selectFriendCircle(friendCircleId);

        //查询点赞人姓名
        Users users = usersService.getById(userId);

        FriendCircleLiked circleLiked = new FriendCircleLiked();
        circleLiked.setFriendCircleId(friendCircleId);
        circleLiked.setLikedUserId(userId);
        circleLiked.setLikedUserName(users.getNickname());
        circleLiked.setBelongUserId(friendCircle.getUserId());
        circleLiked.setCreatedTime(LocalDateTime.now());

        friendCircleLikedMapper.insert(circleLiked);

        //增加被点赞数
        redis.increment(REDIS_FRIEND_CIRCLE_LIKED_COUNTS + ":" + friendCircleId,1);
        //标记哪个用户点赞
        redis.setnx(REDIS_DOES_USER_LIKE_FRIEND_CIRCLE + ":" + friendCircleId + ":" + userId, userId);
    }

    @Transactional
    @Override
    public void unlike(String friendCircleId, String userId) {
        QueryWrapper deleteWrapper = new QueryWrapper<FriendCircleLiked>()
                .eq("friend_circle_id",friendCircleId)
                .eq("liked_user_id",userId);
        friendCircleLikedMapper.delete(deleteWrapper);
        redis.decrement(REDIS_FRIEND_CIRCLE_LIKED_COUNTS + ":" + friendCircleId,1);
        redis.del(REDIS_DOES_USER_LIKE_FRIEND_CIRCLE + ":" + friendCircleId + ":" + userId);
    }

    @Override
    public List<FriendCircleLiked> queryLikedFriends(String friendCircleId) {
        QueryWrapper queryWrapper = new QueryWrapper<FriendCircleLiked>()
                .eq("friend_circle_id",friendCircleId);
        return friendCircleLikedMapper.selectList(queryWrapper);
    }

    @Override
    public boolean doILike(String friendCircleId, String userId) {
        String isExist = redis.get(REDIS_DOES_USER_LIKE_FRIEND_CIRCLE + ":" + friendCircleId + ":" + userId);
        return StringUtils.isNotBlank(isExist);
    }

    @Transactional
    @Override
    public void delete(String friendCircleId, String userId) {
        QueryWrapper<FriendCircle> deleteWrapper = new QueryWrapper<FriendCircle>();
        deleteWrapper.eq("id",friendCircleId);
        deleteWrapper.eq("user_id",userId);
        friendCircleMapper.delete(deleteWrapper);
    }




    private FriendCircle selectFriendCircle(String friendCircleId){
        return friendCircleMapper.selectById(friendCircleId);
    }
}
